/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xml;
import Arbori.Nod;
import Functii.Constanta;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 *
 * @author User
 */
public class SaveXML {
    protected FileWriter fstream;
    protected BufferedWriter out;

    public SaveXML(Nod a, String fisier) throws IOException{
        fstream=new FileWriter(fisier);
        out=new BufferedWriter(fstream);
        out.write("<?xml version=\"10\" encoding=\"ISO-8859-1\" ?>\n");
        save(a,1);
        out.close();
    }
    public void save(Nod a, int nivel) throws IOException{
        int i;
        for(i=1; i<=nivel; i++)
            out.write(" ");
        int index=(a.getClass().getName()).lastIndexOf('.');
        String fin=a.getClass().getName().substring(index+1);
        if(fin.equals("Constanta"))
            out.write("<"+fin+" id=\""+nivel+"\" val=\""+
                    +((Constanta)a).getCt()+"\" >\n");
        else
            out.write("<"+fin+" id=\""+nivel+"\" >\n");
        if(a.getfStang()!=null)
            save(a.getfStang(), nivel+1);
        if(a.getfDrept()!=null)
            save(a.getfDrept(), nivel+1);
        for(i=1; i<=nivel; i++)
            out.write(" ");
        out.write("</"+fin+">"+"\n");
    }
}
